import AjaxForBusOpen from '@/utils/AjaxForBusOpen'
import ReservationDetailModel from '../models/DetailModel'
import PointModel from '../models/PointModel'
import DriverModel from '../models/DriverModel'
import { getUrlOrderNo } from './getUrlOrderNo'
/**
 * 获取订单详情
 *
 */
async function getOrderFromInfo(test = false) {
  let data
  if (test) {
    data = {
      STATE: 2, // 0: 待提交 1：已提交 2：预约成功 -1：取消 -2:操作已受理
      STATE_DESC: '待提交',
      STATE_DETAIL_DESC: '等待支付定金',
      SHOW_BTN_LIST: [1, 2], // 当前状态按钮配置
      ORDER_NO: '201906121650123456',
      TRIP_START_POINT: {
        ADDRESS: '嘉定区中医医院',
        ADDRESS_LNG: '119.652693',
        ADDRESS_LAT: '29.074837'
      },
      VIA_POINT_LIST: [
        {
          ADDRESS: '上海市嘉罗公路10号',
          ADDRESS_LNG: '119.652693',
          ADDRESS_LAT: '29.074837'
        },
        {
          ADDRESS: '上海市嘉罗公路11号',
          ADDRESS_LNG: '119.652693',
          ADDRESS_LAT: '29.074837'
        },
        {
          ADDRESS: '上海市嘉罗公路12号',
          ADDRESS_LNG: '119.652693',
          ADDRESS_LAT: '29.074837'
        }
      ],
      TRIP_END_POINT: {
        ADDRESS: '上海市嘉定区塔城路30号',
        ADDRESS_LNG: '119.652693',
        ADDRESS_LAT: '29.074837'
      },
      TRIP_START_TIME: 1569945600000,
      TRIP_END_TIME: 1569945600000,
      PASSENGER_NUM: 32,
      SUBSCRIBE_CAR_NUM: 3,
      CONTACT: '朱兴庆',
      CONTACT_PHONE: '19965412404',
      CREATE_TIME: 1569945600000,
      SECOND_CONTACT: '朱小明',
      SECOND_CONTACT_PHONE: '18563215513',
      OTHER_EMAIL: '163@qq.com',
      USER_MEMO:
        '准时到准时到准时到准时到准时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到准时到时到 ',
      DRIVER_LIST: [
        {
          LICENSE_NUM_PREFIX: '沪A',
          LICENSE_NUM_SUFFIX: '1234',
          DRIVER_NAME: '吴师傅',
          PHONE: '18654542323'
        },
        {
          LICENSE_NUM_PREFIX: '沪A',
          LICENSE_NUM_SUFFIX: '1234',
          DRIVER_NAME: '张师傅',
          PHONE: '18654542323'
        },
        {
          LICENSE_NUM_PREFIX: '沪A',
          LICENSE_NUM_SUFFIX: '1234',
          DRIVER_NAME: '李师傅',
          PHONE: '18654542323'
        },
        {
          LICENSE_NUM_PREFIX: '沪A',
          LICENSE_NUM_SUFFIX: '1234',
          DRIVER_NAME: '黄师傅',
          PHONE: '18654542323'
        }
      ],
      DEPOSIT_AMOUNT: 16300
    }
  } else {
    let orderNo = getUrlOrderNo()
    let sendObj = {
      ORDER_NO: orderNo
    }
    let res = await AjaxForBusOpen(
      '/chartered-bus-api/chartered/bus_order/detail',
      sendObj
    )
    data = res
  }
  let driverList = []
  if (data.DRIVER_LIST) {
    data.DRIVER_LIST.map(item => {
      let driverModel = new DriverModel(
        item.DRIVER_NAME,
        item.LICENSE_NUM_PREFIX,
        item.LICENSE_NUM_SUFFIX,
        item.PHONE
      )
      driverList.push(driverModel)
    })
  }
  let startPoint = new PointModel(
    data.TRIP_START_POINT.ADDRESS,
    data.TRIP_START_POINT.ADDRESS_LNG,
    data.TRIP_START_POINT.ADDRESS_LAT
  )
  let passbyPointList = []
  if (data.VIA_POINT_LIST) {
    data.VIA_POINT_LIST.map(item => {
      let passbyPoint = new PointModel(
        item.ADDRESS,
        item.ADDRESS_LNG,
        item.ADDRESS_LAT
      )
      passbyPointList.push(passbyPoint)
    })
  }
  let endPoint = new PointModel(
    data.TRIP_END_POINT.ADDRESS,
    data.TRIP_END_POINT.ADDRESS_LNG,
    data.TRIP_END_POINT.ADDRESS_LAT
  )
  let reservationDetailModel = new ReservationDetailModel(
    data.ORDER_NO,
    data.SHOW_BTN_LIST,
    data.STATE,
    data.STATE_DESC,
    data.STATE_DETAIL_DESC,
    startPoint,
    passbyPointList,
    endPoint,
    data.TRIP_START_TIME,
    data.TRIP_END_TIME,
    data.CREATE_TIME,
    data.PASSENGER_NUM,
    data.SUBSCRIBE_CAR_NUM,
    data.CONTACT,
    data.CONTACT_PHONE,
    data.SECOND_CONTACT,
    data.SECOND_CONTACT_PHONE,
    data.OTHER_EMAIL,
    data.USER_MEMO,
    driverList,
    data.DEPOSIT_AMOUNT
  )
  return reservationDetailModel
}

/**
 * 提交评价
 * @param {String} rating  评价等级
 */
async function onSubmitAppraise(rating) {
  let orderNo = getUrlOrderNo()
  let sendObj = {
    ORDER_NO: orderNo,
    RATING: rating
  }
  let res = await AjaxForBusOpen(
    '/chartered-bus-api/chartered/bus_order/evaluation',
    sendObj,
    1
  )
  return res
}

/**
 * 获取预约单状态
 *
 */
function getReservationState() {
  let orderNo = getUrlOrderNo()
  let sendObj = {
    ORDER_NO: orderNo
  }
  return AjaxForBusOpen('/chartered-bus-api/chartered/bus_order/state', sendObj)
}

/**
 * 取消预约单
 *
 */
async function cancelReservation() {
  let orderNo = getUrlOrderNo()
  let sendObj = {
    ORDER_NO: orderNo
  }
  let res = await AjaxForBusOpen(
    '/chartered-bus-api/chartered/bus_order/cancel',
    sendObj,
    1
  )
  return res
}

/**
 * 重新获取支付串
 *
 */
async function getPaymentNo() {
  let orderNo = getUrlOrderNo()
  let sendObj = {
    ORDER_NO: orderNo
  }
  let res = await AjaxForBusOpen(
    '/chartered-bus-api/chartered/bus_order/get/payment',
    sendObj
  )
  let body = {
    payMent: res.PAYMENT,
    payMentStr: res.PAYMENT_STR,
    orderNo: res.ORDER_NO
  }
  return body
}

export {
  getOrderFromInfo,
  onSubmitAppraise,
  getReservationState,
  cancelReservation,
  getPaymentNo
}
